// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//


#pragma once

#include <pollux/dwio/common/selective_floating_point_column_reader.h>
#include <pollux/dwio/dwrf/common/floating_point_decoder.h>

namespace kumo::pollux::dwrf {

template <typename TFile, typename TRequested>
class SelectiveFloatingPointColumnReader
    : public dwio::common::
          SelectiveFloatingPointColumnReader<TFile, TRequested> {
 public:
  using ValueType = TRequested;

  using base =
      dwio::common::SelectiveFloatingPointColumnReader<TFile, TRequested>;

  SelectiveFloatingPointColumnReader(
      const TypePtr& requestedType,
      std::shared_ptr<const dwio::common::TypeWithId> fileType,
      DwrfParams& params,
      common::ScanSpec& scanSpec);

  void seekToRowGroup(int64_t index) override {
    base::seekToRowGroup(index);
    auto positionsProvider = this->formatData_->seekToRowGroup(index);
    decoder_.seekToRowGroup(positionsProvider);
    POLLUX_CHECK(!positionsProvider.hasNext());
  }

  uint64_t skip(uint64_t numValues) override;

  void read(int64_t offset, const RowSet& rows, const uint64_t* incomingNulls)
      override {
    using T = SelectiveFloatingPointColumnReader<TFile, TRequested>;
    this->template readCommon<T, true>(offset, rows, incomingNulls);
    this->readOffset_ += rows.back() + 1;
  }

  template <typename TVisitor>
  void readWithVisitor(const RowSet& rows, TVisitor visitor);

  FloatingPointDecoder<TFile, TRequested> decoder_;
};

template <typename TFile, typename TRequested>
SelectiveFloatingPointColumnReader<TFile, TRequested>::
    SelectiveFloatingPointColumnReader(
        const TypePtr& requestedType,
        std::shared_ptr<const dwio::common::TypeWithId> fileType,
        DwrfParams& params,
        common::ScanSpec& scanSpec)
    : dwio::common::SelectiveFloatingPointColumnReader<TFile, TRequested>(
          requestedType,
          std::move(fileType),
          params,
          scanSpec),
      decoder_(params.stripeStreams().getStream(
          StripeStreamsUtil::getStreamForKind(
              params.stripeStreams(),
              EncodingKey{
                  this->fileType_->id(),
                  params.flatMapContext().sequence},
              proto::Stream_Kind_DATA,
              proto::orc::Stream_Kind_DATA),
          params.streamLabels().label(),
          true)) {}

template <typename TFile, typename TRequested>
uint64_t SelectiveFloatingPointColumnReader<TFile, TRequested>::skip(
    uint64_t numValues) {
  numValues = this->formatData_->skipNulls(numValues);
  decoder_.skip(numValues);
  return numValues;
}

template <typename TFile, typename TRequested>
template <typename TVisitor>
void SelectiveFloatingPointColumnReader<TFile, TRequested>::readWithVisitor(
    const RowSet& /*rows*/,
    TVisitor visitor) {
  if (this->nullsInReadRange_) {
    decoder_.template readWithVisitor<true, TVisitor>(
        this->nullsInReadRange_->template as<uint64_t>(), visitor);
  } else {
    decoder_.template readWithVisitor<false, TVisitor>(nullptr, visitor);
  }
}

} // namespace kumo::pollux::dwrf
